import { getData } from '@vimi/utils-api'
import { dateFormat, excelJsonExport } from '@vimi/utils-tool'
import { SERVICE_NAME } from '@/credit/plugins/constant'

export const enumGuatype = {
  // 提出删除市联担
  2: '直担业务',
  3: '限率业务',
  4: '联担业务',
}

export const enumYesOrNo = {
  0: '否',
  1: '是',
}

// 市场化项目台账
export const ledgerTableColumns = [
  { attrs: { label: '序号', prop: 'index', align: 'center', minWidth: 50 } },
  { attrs: { label: '贷款银行', prop: 'bank_name', align: 'center', minWidth: 160 } },
  {
    attrs: { label: '企业名称/贷款人名称', prop: 'customer_name', align: 'center', minWidth: 200 },
  },
  { attrs: { label: '担保金额(万元)', prop: 'warranty_amount', align: 'center', minWidth: 120 } },
  { attrs: { label: '在保余额(万元)', prop: 'left_funds', align: 'center', minWidth: 120 } },
  { attrs: { label: '担保责任发生日期', prop: 'loadDate', align: 'center', minWidth: 140 } },
  { attrs: { label: '担保责任解除日期', prop: 'loadEndDate', align: 'center', minWidth: 140 } },
  { attrs: { label: '客户类型', prop: 'org_class_name', align: 'center', minWidth: 160 } },
  {
    attrs: { label: '十二大产业分类', prop: 'main_business', align: 'center', minWidth: 200 },
    process: v => v || '-',
  },
  {
    attrs: { label: '主营业务', prop: 'parent_main_business', align: 'center', minWidth: 170 },
    process: v => v || '-',
  },
  {
    attrs: { label: '导入日期', prop: 'create_time', align: 'center', minWidth: 120 },
    process: v => dateFormat(v, 'yyyy-MM-dd'),
  },
  {
    attrs: { label: '营业执照号', prop: 'register_no', align: 'center', minWidth: 140 },
    process: v => v || '-',
  },
  { attrs: { label: '负责人名称', prop: 'corp_name', align: 'center', minWidth: 140 } },
  {
    attrs: { label: '身份证号', prop: 'lender_idcard', align: 'center', minWidth: 140 },
    process: v => v || '-',
  },
  { attrs: { label: '联系电话', prop: 'corp_phone', align: 'center', minWidth: 120 } },

  { attrs: { label: '销售收入(万元)', prop: 'sale_income', align: 'center', minWidth: 120 } },
  { attrs: { label: '资产总额(万元)', prop: 'assets_funds', align: 'center', minWidth: 120 } },
  { attrs: { label: '从业人数(人)', prop: 'emp_total', align: 'center', minWidth: 120 } },

  { attrs: { label: '贷款利率(%)', prop: 'load_rate', align: 'center', minWidth: 100 } },
  {
    attrs: { label: '借款合同号', prop: 'gua_contract', align: 'center', minWidth: 160 },
    process: v => v || '-',
  },
  {
    attrs: { label: '保证合同编号', prop: 'ensure_contract', align: 'center', minWidth: 160 },
    process: v => v || '-',
  },
  {
    attrs: { label: '委托担保合同号', prop: 'entrust_contract', align: 'center', minWidth: 180 },
    process: v => v || '-',
  },

  { attrs: { label: '贷款期限(月)', prop: 'warranty_period', align: 'center', minWidth: 80 } },
  {
    attrs: { label: '反担保措施(详情)', prop: 'warranty_method', align: 'center', minWidth: 160 },
    slot: 'warranty_method',
  },
  {
    attrs: { label: '风险分担比例', prop: 'risk_rate', align: 'center', minWidth: 100 },
    process: v => v || '-',
  },
  { attrs: { label: '是否为扶贫项目', prop: 'isPovertyProject', align: 'center', minWidth: 120 } },
  { attrs: { label: '综合担保费率(%)', prop: 'warranty_rate', align: 'center', minWidth: 100 } },
  {
    attrs: { label: '综合担保费金额(元)', prop: 'warranty_fee', align: 'center', minWidth: 140 },
    process: v => v || '-',
  },
  {
    attrs: {
      label: '联保机构名称',
      prop: 'recommend_service_name',
      align: 'center',
      minWidth: 160,
    },
  },
  { attrs: { label: '项目状态', prop: 'loanBehStatus', align: 'center', minWidth: 120 } },
  { attrs: { label: '贷款用途', prop: 'funds_use_dir', align: 'center', minWidth: 140 } },
  { attrs: { label: '行业分类', prop: 'industrial_name', align: 'center', minWidth: 140 } },
  { attrs: { label: '业务类型', prop: 'guaType', align: 'center', minWidth: 110 } },
  {
    attrs: { label: '备注', prop: 'remark', minWidth: 180 },
    process: v => v || '-',
  },
]

export const reportDataAdapter = (item, index) => {
  item.index = index + 1
  item.isPovertyProject = enumYesOrNo[item.is_poverty_project] || '-'
  item.loanBehStatus = +item.load_beh_status === 6 ? '解保' : '在保'

  item.loadDate = dateFormat(item.load_date, 'yyyy-MM-dd')
  item.loadEndDate = dateFormat(item.load_end_date, 'yyyy-MM-dd')
  item.guaType = enumGuatype[item.gua_type]

  return item
}

export const dataExport = async ({ columns, params, fetch, adapter, filename }) => {
  const { index, size, ...pars } = params
  const [res = {}] = await fetch(pars)

  jsonExport({
    columns,
    data: res.data || [],
    adapter,
    filename,
  })
}

export const jsonExport = async ({ columns, data, adapter, filename }) => {
  // 表头数据
  const header = {}
  // 表头样式
  const headerCellStyles = {}
  // 导出的columns
  const headerColumns = []
  // table process 处理
  const processes = []

  for (let i = 0; i < columns.length; i++) {
    const col = columns[i]
    if (col.operations) {
      continue
    }

    const {
      attrs: { prop, label },
      process,
    } = col
    header[prop] = label
    headerCellStyles[`${String.fromCharCode(65 + i)}1`] = { font: { bold: true } }
    headerColumns.push(prop)

    if (process) {
      processes.push({ prop, process })
    }
  }

  const list = data.map((item, index) => {
    const res = adapter(item, index)
    for (const { prop, process } of processes) {
      item[prop] = process(item[prop])
    }
    return res
  })

  excelJsonExport({
    columns: headerColumns,
    data: [header, ...list],
    filename,
    styles: {
      border: {
        left: { style: 'thin' },
        top: { style: 'thin' },
        right: { style: 'thin' },
        bottom: { style: 'thin' },
      },
      alignment: {
        vertical: 'center',
        horizontal: 'center',
        wrapText: true,
      },
    },
    cellStyles: {
      ...headerCellStyles,
    },
  })
}

export const getCurrentDate = () => {
  const date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1

  month--
  if (month === 0) {
    month = 12
    year--
  }

  return { year, month }
}

// 1、上报时间不固定，但一个月只能上报一次（上报完成后，按钮还显示，点击提示“本月已上报”）
// 2、只能上报上一月的报表，不能上报上上个月及之前的报表
// @tc_20210225
export const getCurrentReport = reportType => {
  const { year, month } = getCurrentDate()
  return getData(SERVICE_NAME, 'report_month', {
    'report_type.eq': reportType,
    'year_number.eq': year,
    'month_number.eq': month,
  }).then(res => {
    return res?.data?.[0]?.report_month_id
  })
}
